package com.kgc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.mapper.EbookCategoryMapper;
import com.kgc.mapper.EbookEntryMapper;
import com.kgc.pojo.EbookCategory;
import com.kgc.pojo.EbookEntry;
import com.kgc.pojo.EbookEntryExample;
import com.kgc.service.SelectAllService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SelectAllServiceImpl implements SelectAllService {
    //引入mapper
    @Autowired
    private EbookEntryMapper ebookEntryMapper;

    @Autowired
    private EbookCategoryMapper ebookCategoryMapper;


    public PageInfo<EbookEntry> selectEbookEntryById(Integer categoryId, Integer pageNo, Integer pageSize) {
        //创建条件类
        EbookEntryExample ebookEntryExample = new EbookEntryExample();
        EbookEntryExample.Criteria criteria = ebookEntryExample.createCriteria();

        //拼接条件
        if(categoryId != 0){
            criteria.andCategoryIdEqualTo(categoryId);
        }

        // 开启分页
        PageHelper.startPage(pageNo,pageSize);

        //开始查询
        List<EbookEntry> ebookEntries = ebookEntryMapper.selectByExample(ebookEntryExample);

        for (EbookEntry ebookEntry : ebookEntries) {
            EbookCategory ebookCategory = ebookCategoryMapper.selectByPrimaryKey(ebookEntry.getCategoryId());

            ebookEntry.setCategoryId(ebookCategory.getId());
        }

        //将数据放到分页工具中
        PageInfo<EbookEntry> pageInfo = new PageInfo<EbookEntry>(ebookEntries);
        return pageInfo;
    }
}
